import { log } from 'console';
import { AppDataSource } from './data.source'
import { User } from './entity/User'
import { first } from 'rxjs';

// 首先初始化
AppDataSource.initialize().then(async () => {
   // 获取User实体类的存储仓库 
   const userRepository = AppDataSource.getRepository(User);

   const user1 = new User();
   user1.firstName = 'firstName01'
   user1.lastName = 'lastName01'
   user1.age = 18
   user1.email = (new Date().getTime() + 1) + '@qq.com'
   user1.isActive = true

   // 新增一个用户
   await userRepository.save(user1);

   // 查询
   const users = await userRepository.find()
   console.log(users);

   // 查询单个 findOne
   const userFind1 = await userRepository.findOne({ where: { id: 10 } })
   console.log(userFind1);

   // 更新
   user1.age = 80;
   await userRepository.save(user1);      //重新保存也可更新

   // 删除
   await userRepository.remove(userFind1);
})